﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenSource.Controls
{
    public enum InformationType
    {
        Input,
        OutPut,
        Link
    }


    public enum ListType
    {
        DropDownList,
        ListBox
    }

    public class InformationCell : InputFormCell
    {

        private PropertyDescriptor textFieldDesc;

       

        [Description("ButtonField_Text")]
        [Localizable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        public virtual string Text
        {
            get { return (string)this.ViewState["Text"] ?? string.Empty; }
            set
            {
                if (object.Equals((object)value, this.ViewState["Text"]))
                    return;
                this.ViewState["Text"] = (object)value;
                this.OnFieldChanged();
            }
        }

     
        [Description("ButtonField_Text")]
        [Localizable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        public bool CanUpdate
        {
            get
            {
                if (this.ViewState["CanUpdate"] != null)
                    return (bool)this.ViewState["CanUpdate"];
                else
                    return false;
            }
            set
            {
                if (object.Equals((object)value, this.ViewState["CanUpdate"]))
                    return;
                this.ViewState["CanUpdate"] = (object)value;
                //this.OnFieldChanged();
            }
        }

        /// <devdoc>
        /// <para>Indicates the button type for the field.</para>
        /// </devdoc>       
        [Category("Appearance"),
         DefaultValue(ListType.DropDownList),
         Description("SR.ButtonFieldBase_ButtonType")]
        public virtual InformationType InformationType
        {
            get
            {
                object o = ViewState["InformationType"];
                if (o != null)
                    return (InformationType)o;
                return InformationType.OutPut;
            }
            set
            {
                if (value < InformationType.Input || value > InformationType.Link)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                object oldValue = ViewState["InformationType"];
                if (oldValue == null || (InformationType)oldValue != value)
                {
                    ViewState["InformationType"] = value;
                    OnFieldChanged();
                }
            }
        }



        protected override void CopyProperties(InputFormCell newField)
        {
            //((in)newfoField).CommandName = this.CommandName;
            //((SelectCell)newField).DataTextField = this.DataTextField;
            //((SelectCell)newField).DataTextFormatString = this.DataTextFormatString;
            //((SelectCell)newField).ImageUrl = this.ImageUrl;
            ((InformationCell)newField).Text = this.Text;
            base.CopyProperties(newField);
        }

     
        protected override InputFormCell CreateField()
        {
            return (InputFormCell)new InformationCell();
        }


        public override bool Initialize(bool sortingEnabled, Control control)
        {
            base.Initialize(sortingEnabled, control);
            this.textFieldDesc = (PropertyDescriptor)null;
            return false;
        }

        public override void InitializeCell(InputFormCellField cell)
        {
            base.InitializeCell(cell);
            IPostBackContainer container = this.Control as IPostBackContainer;
            WebControl control = null;

            switch (this.InformationType)
            {
                case InformationType.Input:
                    TextBox txt = new TextBox();
                    txt.Text = this.Text;
                    control = txt;
                    break;
                case InformationType.OutPut:
                    Label lbl = new Label();
                    lbl.Text = this.Text;
                    control = lbl;
                    break;
                case InformationType.Link:
                    HyperLink link = new HyperLink();
                    link.Text = this.Text;
                    control = link;
                    break;
            }
            cell.Controls.Add((Control) control);
        }




        private void OnDataBindField(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            Control namingContainer = control.NamingContainer;
            //if (namingContainer == null)
            //    throw new HttpException(System.Web.SR.GetString("DataControlField_NoContainer"));
            object dataItem = DataBinder.GetDataItem((object)namingContainer);
            //if (dataItem == null && !this.DesignMode)
            //    throw new HttpException(System.Web.SR.GetString("DataItem_Not_Found"));
            if (this.textFieldDesc == null && dataItem != null)
            {
                string dataTextField = ""; // this.DataTextField;
                this.textFieldDesc = TypeDescriptor.GetProperties(dataItem).Find(dataTextField, true);
                //if (this.textFieldDesc == null && !this.DesignMode)
                //    throw new HttpException(System.Web.SR.GetString("Field_Not_Found", new object[1]
                {
                    //  (object) dataTextField
                    //}));
                }
                //    string str = this.textFieldDesc == null || dataItem == null ? System.Web.SR.GetString("Sample_Databound_Text") : this.FormatDataTextValue(this.textFieldDesc.GetValue(dataItem));
                //    ((IButtonControl)control).Text = str;
                //}
            }

            //buttonControl.DataSource = GetSource();
            //buttonControl.DataBind();


            // ((DataBoundControl) sender).DataSource = dataItem;//

            //db = (DataBoundControl)control;

            //db.DataSource = dataItem;

            //db.DataBind();
        }

        public override void ValidateSupportsCallback()
        {
        }

    }
}
